import { execute } from "../../database/connector";

import { MachineQueries } from "../queries/machine.query";
import { IMachine } from "../../models/machine.model";

/**
 * gets machine list
 */
export const getMachines = async () => {
  return execute<IMachine[]>(MachineQueries.GetMachines, []);
};


/**
* gets active machine list
*/
export const getActiveMachines = async () => {
 return execute<IMachine[]>(MachineQueries.GetActiveMachines, []);
};

/**
 * gets a machine based on id provided
 */
export const getMachineById = async (id: IMachine['id']) => {
  return execute<IMachine>(MachineQueries.GetMachineById, [id]);
};

/**
 * adds a new active machine record
 */
export const insertMachine = async (machine: IMachine) => {
  const result = await execute<{ affectedRows: number }>(MachineQueries.AddMachine, [
    [Object.values(machine)]
  ]);
  return result.affectedRows > 0;
};

/**
 * updates machine information based on the id provided
 */
export const updateMachine = async (machine: IMachine) => {
  const result = await execute<{ affectedRows: number }>(MachineQueries.UpdateMachineById, [
    machine,
    machine.id
  ]);
  return result.affectedRows > 0;
};

/**
 * updates machine information based on the id provided
 */
 export const deleteMachine = async (id: IMachine['id']) => {
  const result = await execute<{ affectedRows: number }>(MachineQueries.DeleteMachineById, [
    id
  ]);
  return result.affectedRows > 0;
};